#include "PID_SetWin.h"

/*创建一个键盘事件组*/
static lv_group_t* keyGroup;

//三个频率输入框
static lv_obj_t * spinbox_kp;
static lv_obj_t * spinbox_ki;
static lv_obj_t * spinbox_kd;
//一个开关组件
static lv_obj_t * sw;

/**退出本界面回调函数**/
static void exit_screen(void* scr)
{
	lv_obj_del(scr);
}


/**"Enter"按键回调函数**/
static void enter_btn_cb(lv_event_t * e)
{
	//保存并退出
	motor_set_PIDenable(lv_obj_has_state(sw, LV_STATE_CHECKED));
	printf("PID state %d\r\n",motor_IsPIDenable());
	motor_set_pid_Kp(lv_spinbox_get_value(spinbox_kp)/100.0);
	motor_set_pid_Ki(lv_spinbox_get_value(spinbox_ki)/100.0);
	motor_set_pid_Kd(lv_spinbox_get_value(spinbox_kd)/100.0);
	lv_group_del(keyGroup);
	lv_group_set_default(NULL);
	lv_async_call(exit_screen, lv_scr_act());
	menu(3);
}

/**"Cancel"按钮回调函数**/
static void cancel_btn_cb(lv_event_t * e)
{
	printf("PID state %d\r\n",motor_IsPIDenable());
	lv_group_del(keyGroup); //不保存退出
	lv_group_set_default(NULL);
	lv_async_call(exit_screen, lv_scr_act());
	menu(3);
}

/**切换数字输入框的编辑状态 回调函数**/
static void spinbox_cb(lv_event_t * event)
{
    /**判断是否是编辑状态**/
	if(lv_group_get_editing(keyGroup))
	{
		//printf("LV_STATE_FOCUSED\r\n");
		lv_group_set_editing(keyGroup, false);
	}
	else
	{
		//printf("!LV_STATE_FOCUSED\r\n");
		lv_group_set_editing(keyGroup, true);
		//printf("set LV_STATE_FOCUSED\r\n");
	}
}


void open_pidSetWin()
{

    lv_obj_t*  scr = lv_obj_create(NULL);
	lv_scr_load(scr);

	sw = lv_switch_create(lv_scr_act());
    lv_obj_align(sw,LV_ALIGN_TOP_LEFT,65,5);
    lv_obj_set_height(sw,12);
    lv_obj_set_width(sw,25);

	lv_obj_clear_state(sw,LV_STATE_CHECKED);
	lv_obj_clear_state(sw,LV_STATE_CHECKED);
    lv_obj_t* label_pid = lv_label_create(lv_scr_act());
    lv_label_set_text(label_pid,"PID调节");
    lv_obj_align(label_pid,LV_ALIGN_TOP_LEFT,0,0);
    lv_obj_set_style_text_font(label_pid,&yahei_16,LV_PART_MAIN);

    lv_obj_t* label = lv_label_create(lv_scr_act());
    lv_label_set_text(label, "Kp:");
    lv_obj_align(label,LV_ALIGN_TOP_LEFT,0,20);

    label = lv_label_create(lv_scr_act());
    lv_label_set_text(label, "Ki:");
    lv_obj_align(label,LV_ALIGN_TOP_LEFT,0,40);

    label = lv_label_create(lv_scr_act());
    lv_label_set_text(label, "Kd:");
    lv_obj_align(label,LV_ALIGN_TOP_LEFT,0,60);

/***设置pid三个参数的数字输入框*****/
    spinbox_kp = lv_spinbox_create(lv_scr_act());
    lv_obj_align(spinbox_kp,LV_ALIGN_TOP_LEFT,27,23);
    lv_spinbox_set_range(spinbox_kp, -10000, 10000);
    lv_spinbox_set_digit_format(spinbox_kp, 5, 3);
	lv_spinbox_set_step(spinbox_kp,1);
    lv_obj_set_width(spinbox_kp,70);
    lv_obj_set_height(spinbox_kp,16);
    lv_obj_set_style_pad_bottom(spinbox_kp,0,LV_PART_MAIN);
    lv_obj_set_style_pad_top(spinbox_kp,0,LV_PART_MAIN);
	lv_spinbox_set_value(spinbox_kp, motor_get_pid_Kp()*100);
	lv_obj_add_event_cb(spinbox_kp,spinbox_cb,LV_EVENT_PRESSED,NULL);

    spinbox_ki = lv_spinbox_create(lv_scr_act());
    lv_obj_align(spinbox_ki,LV_ALIGN_TOP_LEFT,27,43);
    lv_spinbox_set_range(spinbox_ki, -10000, 10000);
    lv_spinbox_set_digit_format(spinbox_ki, 5, 3);
	lv_spinbox_set_step(spinbox_ki,1);
    lv_obj_set_width(spinbox_ki,70);
    lv_obj_set_height(spinbox_ki,16);
    lv_obj_set_style_pad_bottom(spinbox_ki,0,LV_PART_MAIN);
    lv_obj_set_style_pad_top(spinbox_ki,0,LV_PART_MAIN);
	lv_spinbox_set_value(spinbox_ki, motor_get_pid_Ki()*100); 
	lv_obj_add_event_cb(spinbox_ki,spinbox_cb,LV_EVENT_PRESSED,NULL);  

    spinbox_kd = lv_spinbox_create(lv_scr_act());
    lv_obj_align(spinbox_kd,LV_ALIGN_TOP_LEFT,27,63);
    lv_spinbox_set_range(spinbox_kd, -10000, 10000);
    lv_spinbox_set_digit_format(spinbox_kd, 5, 3);
	lv_spinbox_set_step(spinbox_kd,1);
    lv_obj_set_width(spinbox_kd,70);
    lv_obj_set_height(spinbox_kd,16);
    lv_obj_set_style_pad_bottom(spinbox_kd,0,LV_PART_MAIN);
    lv_obj_set_style_pad_top(spinbox_kd,0,LV_PART_MAIN);
	lv_spinbox_set_value(spinbox_kd, motor_get_pid_Kd()*100); 
	lv_obj_add_event_cb(spinbox_kd,spinbox_cb,LV_EVENT_PRESSED,NULL);

	lv_obj_t* btn1 = lv_btn_create(lv_scr_act());
	label = lv_label_create(btn1);
	lv_label_set_text(label, "OK");
	//lv_obj_align_to(label, btn1, LV_ALIGN_CENTER, 0, -5);
	lv_obj_set_size(btn1, 42, 26);
	lv_obj_align(btn1, LV_ALIGN_TOP_LEFT, 105, 22);
	lv_obj_add_event_cb(btn1, enter_btn_cb, LV_EVENT_PRESSED, NULL);

	lv_obj_t* btn2 = lv_btn_create(lv_scr_act());
	label = lv_label_create(btn2);
	lv_label_set_text(label, "Esc");
	//lv_obj_align_to(label, btn2, LV_ALIGN_CENTER, 0, -5);
	lv_obj_set_size(btn2, 42, 26);
	lv_obj_align(btn2, LV_ALIGN_TOP_LEFT, 105, 52);
	lv_obj_add_event_cb(btn2, cancel_btn_cb, LV_EVENT_PRESSED, NULL); 

	keyGroup = lv_group_create();
	lv_indev_set_group(indev_keypad, keyGroup);
	lv_group_set_default(keyGroup);

	lv_group_add_obj(keyGroup,label_pid);
	lv_group_add_obj(keyGroup,sw);
	lv_group_add_obj(keyGroup,spinbox_kp);
	lv_group_add_obj(keyGroup,spinbox_ki);
	lv_group_add_obj(keyGroup,spinbox_kd);
	lv_group_add_obj(keyGroup, btn1);
	lv_group_add_obj(keyGroup, btn2);

    if(motor_IsPIDenable())
    {
		printf("add state CHECKED\r\n");
        lv_obj_add_state(sw,LV_STATE_CHECKED);
    }
    else
    {
		lv_obj_clear_state(sw,LV_STATE_CHECKED);
		printf("clear state CHECKED\r\n");
    }
}

